﻿#include "RegisterCenter_Device.h"

#include "Device.h"
#include "HeartBeatCodec.h"
#include "MsgIdManager.h"
#include "TurnTableCodec.h"

RegisterCenter_Device::RegisterCenter_Device()
{

}

RegisterCenter_Device::~RegisterCenter_Device()
{
  for(int deviceId : deviceMap.keys())
    {
      delete deviceMap.take(deviceId);
    }
}

bool RegisterCenter_Device::isOnline(uint deviceId)
{
  return deviceMap.contains(deviceId);
}

void RegisterCenter_Device::updateLifeTime(uint deviceId, ushort msgId, uint ip, ushort port, int lifeTime_ms)
{
  (void)msgId;
  Device* device=deviceMap.value(deviceId);
  !ip?0:device->setHostAddress(ip);
  !port?0:device->setPort(port);
  device->setLifeTime_ms(0<=lifeTime_ms?defaultLifeTime:lifeTime_ms);
}

Device* RegisterCenter_Device::getDeviceById(uint deviceId)
{
  return deviceMap.value(deviceId,Q_NULLPTR);
}

void RegisterCenter_Device::deviceLogin(Device* device)
{
  int deviceId=device->getDeviceId();
  if(deviceMap.contains(deviceId))
    {
      deviceMap.take(deviceId)->deleteLater();
    }
  deviceMap.insert(device->getDeviceId(),device);
  connect(device,&Device::signal_lifeTimeOut,this,&RegisterCenter_Device::slot_lifeTimeout);
  connect(device,&Device::signal_heartBeatTimeout,this,&RegisterCenter_Device::slot_heartBeatTimeout);
  //  connect(device,&Device::signal_currentStateTimeout,this,&RegisterCenter_Device::slot_currentStateTimeout);
  //  connect(device,&Device::signal_runningStateTimeout,this,&RegisterCenter_Device::slot_runningStateTimeout);
  device->setLifeTime_ms(defaultLifeTime);
  device->setHeartBeatInterval_ms(defaultHeartBeatTime);
//  device->setCurrentStateInterval(defaultCurrentStateTime);
//  device->setRunningStateInterval(defaultRunningStateTime);
  logSaver->saveLog("RegisterCenter_Device,device "+QString::number(deviceId)+" registered");
  emit signal_deviceOnline(device);
}

void RegisterCenter_Device::deviceLogout(uint deviceId)
{
  if(!deviceMap.contains(deviceId))
    {
      return;
    }
  deviceMap.take(deviceId)->deleteLater();
}

void RegisterCenter_Device::slot_lifeTimeout(Device *device)
{
  logSaver->saveLog("RegisterCenter_Device,device "+QString::number(device->getDeviceId())+" timeout");
  emit signal_deviceOffline(device);
  deviceLogout(device->getDeviceId());
}

void RegisterCenter_Device::slot_heartBeatTimeout(Device *device)
{
  heartBeatCodec->sendHeartBeat(device->getDeviceId(),MsgIdManager::getMsgId());
}

void RegisterCenter_Device::slot_currentStateTimeout(Device *device)
{
  turnTableCodec->updateCurrentState(device->getDeviceId(),MsgIdManager::getMsgId());
}

void RegisterCenter_Device::slot_runningStateTimeout(Device *device)
{
  turnTableCodec->updateRunningState(device->getDeviceId(),MsgIdManager::getMsgId());
}

int RegisterCenter_Device::getDefaultLifeTime()
{
  return defaultLifeTime;
}

int RegisterCenter_Device::getDefaultHeartBeat()
{
  return defaultHeartBeatTime;
}

HeartBeatCodec* RegisterCenter_Device::getHeartBeatCodec()const
{
  return heartBeatCodec;
}

RegisterCenter_Device* RegisterCenter_Device::setHeartBeatCodec(HeartBeatCodec*const heartBeatCodec)
{
  this->heartBeatCodec=heartBeatCodec;
  printInjection(this,heartBeatCodec);
  return this;
}
TurnTableCodec* RegisterCenter_Device::getTurnTableCodec()const
{
  return turnTableCodec;
}

RegisterCenter_Device* RegisterCenter_Device::setTurnTableCodec(TurnTableCodec*const turnTableCodec)
{
  this->turnTableCodec=turnTableCodec;
  printInjection(this,turnTableCodec);
  return this;
}

void RegisterCenter_Device::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setHeartBeatCodec(getObjFromMap<HeartBeatCodec>(objMap));
  setTurnTableCodec(getObjFromMap<TurnTableCodec>(objMap));
}
